@using System.Text.Json.Nodes
@using Elsa.Api.Client.Resources.WorkflowDefinitions.Models
@inherits BackendComponentBase

<ThemedComponentWrapper>
    <WorkflowDefinitionEditor DefinitionId="@DefinitionId"
                              WorkflowDefinitionExecuted="WorkflowDefinitionExecuted"
                              WorkflowDefinitionVersionSelected="WorkflowDefinitionVersionSelected"
                              ActivitySelected="ActivitySelected"/>
</ThemedComponentWrapper>

@code
{
    private bool _shouldRender;
    private string _definitionId = default!;

    /// The ID of the workflow definition to edit.
    [Parameter] public string DefinitionId
    {
        get => _definitionId;
        set
        {
            if (_definitionId == value) return;
            _definitionId = value;
            _shouldRender = true;
        }
    }

    /// <summary>An event that is invoked when a workflow definition has been executed.</summary>
    /// <remarks>The ID of the workflow instance is provided as the value to the event callback.</remarks>
    [Parameter] public Func<string, Task>? WorkflowDefinitionExecuted { get; set; }

    /// Gets or sets the event that occurs when the workflow definition version is updated.
    [Parameter] public Func<WorkflowDefinition, Task>? WorkflowDefinitionVersionSelected { get; set; }

    /// Gets or sets the event triggered when an activity is selected.
    [Parameter] public Func<JsonObject, Task>? ActivitySelected { get; set; }

    /// Gets the currently selected activity ID.
    public string? SelectedActivityId => WorkflowDefinitionEditor.SelectedActivityId;

    private WorkflowDefinitionEditor WorkflowDefinitionEditor { get; set; } = default!;

    /// Gets the currently selected workflow definition version.
    public WorkflowDefinition? GetSelectedWorkflowDefinitionVersion() => WorkflowDefinitionEditor.GetSelectedWorkflowDefinitionVersion();

    /// <inheritdoc />
    protected override bool ShouldRender()
    {
        if (!_shouldRender) return false;
        _shouldRender = false;
        return true;
    }
}